package jobs;

import java.io.File;

import models.App;
import models.Level;
import models.Question;
import models.User;

import play.Logger;
import play.jobs.Job;
import play.jobs.OnApplicationStart;
import play.libs.Codec;

@OnApplicationStart
public class StartJob extends Job {

	public void doJob() {
		Logger.info("PlayLab已启用");

		File uploadFile = new File("upload");
		if (!uploadFile.exists()) {
			uploadFile.mkdir();
			Logger.info("图片上传目录创建成功");
		}
		
		File iconFile=new File(new File("upload"),"icon");
		if(!iconFile.exists()){
			iconFile.mkdir();
			Logger.info("应用图标上传目录创建成功");
		}
		

		// 创建管理员
		User newuser = new User("dhl@oopsplay.org").save();
		newuser.password = Codec.hexMD5("123456");
		newuser.name = "丁海亮";
		newuser.title = "Oopsplay创始人";
		newuser.introduction = "Architect of Azrue Ltd.从事互联网工作6年，09年创办自己的公司。";
		newuser.addLink("Oopsplay官方", "www.oopsplay.org");
		newuser.addLink("谷歌", "www.google.com");
		// newuser.level=1;
		Level level = new Level("管理员").save();
		newuser.setLevel(level);
		newuser.save();
		Logger.info(newuser.links.get(0).url);

		// newuser.links.add(link1);
		// 每次更新都要手动保存数据
		newuser.save();
		if (newuser.isPersistent()) {
			Logger.info(newuser.password);
		}

		// 创建角色
		new Level("见习员").save();
		new Level("实验员").save();
		new Level("研究员").save();
		Logger.info(Level.count() == 4 ? "角色创建成功" : "角色创建失败");

		// 创建题库
		new Question("1+5=?", "6").save();
		Logger.info("问题的数量：" + Question.count());

		// 创建App
		App application = new App("PlayLab",
				"用于人事管理和项目展示");
		application.addUser(newuser);
		application.verified="1";
		application.icon="/public/images/app.gif";
		application.save();
		//已通过审核
		newuser.save();
		Logger.info("App创建成功");

	}

}